Datum levenshtein_distance(PG_FUNCTION_ARGS)
{
	text *str_01 = PG_GETARG_DATUM(0);
	text *txt_02 = PG_GETARG_DATUM(1);
	int32 result = 1; 
	char *a = VARDATA_ANY(str_01), *b = VARDATA_ANY(txt_02);
	char d[287][287];
	int m = VARSIZE_ANY_EXHDR(str_01), n = VARSIZE_ANY_EXHDR(txt_02);
	for (int i = 0; i < m + 1; i++)
		d[i][0] = i;
	for (int i = 0; i < n + 1; i++)
		d[0][i] = i;

	for (int i = 1; i < m + 1; i++)
		for (int j = 1; j < n + 1; j++)
			if (a[i - 1] == b[j - 1])
				d[i][j] = d[i - 1][j - 1];
			else
			{
				int tmp = d[i-1][j] + 1 < d[i][j-1] + 1 ? d[i-1][j] + 1 : d[i][j-1] + 1;
				d[i][j] = tmp < d[i-1][j-1] + 1 ? tmp : d[i-1][j-1] + 1;
			}
	result = (int)d[m][n];
	PG_RETURN_INT32(result);
}
